/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package me.panpf.sketch.sample.widget.page;

import me.panpf.sketch.SketchImageView;
import me.panpf.sketch.sample.AssetImage;
import me.panpf.sketch.sample.ResourceTable;
import me.panpf.sketch.util.Utils;
import ohos.agp.components.*;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.app.Context;
import ohos.media.image.common.Size;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

/**
 * ResizeImageProcessorTestPageView
 */
public class ResizeImageProcessorTestPageView extends AbstractPageView {
    private Text mTextResizeWidth;
    private Text mTextResizeHeight;
    private Slider mSeekBarResizeWidth;
    private Slider mSeekBarResizeHeight;
    private Button currentCheckedButton;
    private SketchImageView sketchImageView;
    private Map<String, Image.ScaleMode> scaleTypeMap;

    private int mImageWidth = 0;
    private int mImageHeight = 0;

    public ResizeImageProcessorTestPageView(Context context, String name) {
        super(context, name);
    }

    @Override
    public void initView() {
        super.setRootView(loadView());
        sketchImageView = (SketchImageView) getRootView().findComponentById(ResourceTable.Id_image_resize);
        sketchImageView.setBindStateChangedListener(new Component.BindStateChangedListener() {
            @Override
            public void onComponentBoundToWindow(Component component) {
                mImageWidth = component.getRight() - component.getLeft();
                mImageHeight = component.getBottom() - component.getTop();
                apply();
            }

            @Override
            public void onComponentUnboundFromWindow(Component component) {

            }
        });
        mTextResizeWidth = (Text) getRootView().findComponentById(ResourceTable.Id_text_resize_width);
        mTextResizeHeight = (Text) getRootView().findComponentById(ResourceTable.Id_text_resize_height);
        mSeekBarResizeWidth = (Slider) getRootView().findComponentById(ResourceTable.Id_seekBar_resize_width);
        setSeekBarWidthParam();
        mSeekBarResizeHeight = (Slider) getRootView().findComponentById(ResourceTable.Id_seekBar_resize_height);
        setSeekBarHeightParam();
        setBtnAction();
    }

    private void setSeekBarWidthParam() {
        mSeekBarResizeWidth.setProgressValue(50);
        mSeekBarResizeWidth.setValueChangedListener(new Slider.ValueChangedListener() {
            @Override
            public void onProgressUpdated(Slider slider, int i1, boolean b) {
                int progress = 20;
                if (i1 < progress) {
                    mSeekBarResizeWidth.setProgressValue(progress);
                } else {
                    progress = i1;
                }
                mTextResizeWidth.setText(String.format("%d/%d", progress * 10, 1000, Locale.ENGLISH));
            }

            @Override
            public void onTouchStart(Slider slider) {

            }

            @Override
            public void onTouchEnd(Slider slider) {
                apply();
            }
        });
    }

    private void setSeekBarHeightParam() {
        mSeekBarResizeHeight.setProgressValue(50);
        mSeekBarResizeHeight.setValueChangedListener(new Slider.ValueChangedListener() {
            @Override
            public void onProgressUpdated(Slider slider, int i1, boolean b) {
                int progress = 20;
                if (i1 < progress) {
                    mSeekBarResizeHeight.setProgressValue(progress);
                } else {
                    progress = i1;
                }
                mTextResizeHeight.setText(String.format("%d/%d", progress * 10, 1000, Locale.ENGLISH));
            }

            @Override
            public void onTouchStart(Slider slider) {

            }

            @Override
            public void onTouchEnd(Slider slider) {
                apply();
            }
        });
    }

    private void setBtnAction() {
        Component.ClickedListener buttonOnClickListener = component -> {
            Button btn = (Button) component;
            btn.setEnabled(false);
            btn.setBackground(new ShapeElement(getRootView().getContext(), ResourceTable.Graphic_button_element_pressed));
            btn.setTextColor(new Color(Color.rgb(168, 168, 168)));
            if (currentCheckedButton != null) {
                currentCheckedButton.setEnabled(true);
                currentCheckedButton.setBackground(new ShapeElement(getRootView().getContext(), ResourceTable.Graphic_button_element_normal));
                currentCheckedButton.setTextColor(Color.BLACK);
            }
            currentCheckedButton = btn;
            apply();
        };
        scaleTypeMap = new HashMap<String, Image.ScaleMode>();
        getRootView().findComponentById(ResourceTable.Id_button_resize_zoom_start).setClickedListener(buttonOnClickListener);
        scaleTypeMap.put("Id_button_resize_zoom_start", Image.ScaleMode.ZOOM_START);
        Button btn = (Button) getRootView().findComponentById(ResourceTable.Id_button_resize_zoom_center);
        btn.setClickedListener(buttonOnClickListener);
        scaleTypeMap.put("Id_button_resize_zoom_center", Image.ScaleMode.ZOOM_CENTER);
        getRootView().findComponentById(ResourceTable.Id_button_resize_zoom_end).setClickedListener(buttonOnClickListener);
        scaleTypeMap.put("Id_button_resize_zoom_end", Image.ScaleMode.ZOOM_END);
        getRootView().findComponentById(ResourceTable.Id_button_resize_stretch).setClickedListener(buttonOnClickListener);
        scaleTypeMap.put("Id_button_resize_stretch", Image.ScaleMode.STRETCH);
        getRootView().findComponentById(ResourceTable.Id_button_resize_center).setClickedListener(buttonOnClickListener);
        scaleTypeMap.put("Id_button_resize_center", Image.ScaleMode.CENTER);
        getRootView().findComponentById(ResourceTable.Id_button_resize_inside).setClickedListener(buttonOnClickListener);
        scaleTypeMap.put("Id_button_resize_inside", Image.ScaleMode.INSIDE);
        getRootView().findComponentById(ResourceTable.Id_button_resize_clip_center).setClickedListener(buttonOnClickListener);
        scaleTypeMap.put("Id_button_resize_clip_center", Image.ScaleMode.CLIP_CENTER);
        btn.simulateClick();
    }

    private Component loadView() {
        ComponentContainer layout = (ComponentContainer) LayoutScatter.getInstance(super.getContext()).parse
                (ResourceTable.Layout_resize_processor_page_view, null, false);

        return layout;
    }

    private void apply() {
        if (mImageWidth == 0 || mImageHeight == 0) {
            return;
        }
        Image.ScaleMode scaleType = scaleTypeMap.get(currentCheckedButton.getName());
        Size size = Utils.getPixmapSize(mSeekBarResizeWidth.getProgress() * 10, mSeekBarResizeHeight.getProgress() * 10,
                mImageWidth, mImageHeight);
        sketchImageView.getOptions().setResize(size.width, size.height, scaleType);
        sketchImageView.displayAssetImage(AssetImage.MEI_NV);
    }
}
